from app.services.business.gaolu_diagnose.ind.abstract_ind import AbstractInd


class CG_LT_GL_GL04_RChanliang(AbstractInd):

    def get_name(self):
        return "CG_LT_GL_GL04_RChanliang"

    def get_data_source(self):
        return "mysql"

    def get_sql(self, params):
        return """      
            SELECT
            SUM(tieliang) as CG_LT_GL_GL04_RChanliang
            FROM(
            SELECT
            t1.fnumber,
            t1.fdate,
            t2.tieliang
            FROM
            (SELECT
             cg.syn_tq_rgaoluchutie.fnumber,
             cg.syn_tq_rgaoluchutie.fdate
            FROM  cg.syn_tq_rgaoluchutie  where fdate =DATE_SUB(CURDATE(), INTERVAL 1 DAY))t1 
            JOIN 
             (SELECT
            fnumber,
            SUM(fquantity) as tieliang
            FROM cg.syn_tq_rtieguan where CAST(fdatetime as date) >DATE_SUB(CURDATE(), INTERVAL 2 DAY)
            GROUP BY fnumber)t2
            on t1.fnumber = t2.fnumber)t3

        """.format(**params)

    @AbstractInd.cache_wrapper
    def get_result(self):
        sql: str = self.get_sql(self.params)
        cursor = self.mysql_cursor
        cursor.execute(sql)
        result = cursor.fetchone()

        if result is not None:
            return result['CG_LT_GL_GL04_RChanliang']

        return 0